//
//  BaseImagePickerController.swift
//  icar
//
//  Created by gongkai on 15/4/9.
//  Copyright (c) 2015年 anytracking. All rights reserved.
//

import UIKit

class BaseImagePickerController: BaseViewController,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    
    /**
    图片选择，从相册或拍照
    **/
    func takePicker() {
        var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从手机选择","拍照")
        sheet.showInView(self.view)
    }
    
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        if(buttonIndex==1){//相册
            self.takePicker(.PhotoLibrary)
        }else if(buttonIndex==2){//拍照
            self.takePicker(.Camera)
        }
    }
    
    /**
    哪种方式选择图片
    **/
    func takePicker(sourceType:UIImagePickerControllerSourceType){
        var mediatypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType)
        if(UIImagePickerController.isSourceTypeAvailable(sourceType) && count(mediatypes!) > 0){
            var mediatypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
            var picker = UIImagePickerController()
            picker.mediaTypes = mediatypes!
            picker.delegate = self
            picker.allowsEditing = true
            picker.sourceType=sourceType
            self.presentViewController(picker, animated: true, completion: nil)
        }else{
            Hinter.alert("当前设备不支持拍摄功能");
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
}
